<?php
Globals::requireTable('Acl');
Globals::requireTable('AclResource');

class AclModel extends Config
{
	protected $aclTable;
	protected $aclResourceTable;
	
	public function __construct($config = null)
	{
		parent::__construct($config);
		
		$this->aclTable			= new AclTable($config);
		$this->aclResourceTable	= new AclResourceTable($config);
	}
	
	public function getTable()
	{
		return $this->aclTable;
	}
	
	public function isAllowed($groupid, $resourcename)
	{
		$resourceRow = $this->aclResourceTable->getRow(array('name' => $resourcename));
		
		if (!$resourceRow)
		{
			$this->message = '权限对象('.$resourcename.')不存在。';
			return false;
		}
		
		$aclRow = $this->aclTable->getRow(array('groupid' => $groupid, 'resourceid' => $resourceRow['id']));
		
		if (!$aclRow)
		{
			$this->message = '您没有'.$resourceRow['description'].'权限。';
			return false;
		}
		
		return true;
	}
}
